#!/bin/sh


# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later
# Isidro Catalan <skarvin@gmail.com>



# ----------------------------------------------------------------------------
# desc: Librearia de gestión para el demonio Crond
# author: Isidro Catalan <skarvin@gmail.com>, http://www.opendomo.com
# date: Noviembre 2008
#
# CHANGES:
#
# ----------------------------------------------------------------------------

# Common video module vars
. "/etc/opendomo/videoConfVars.conf"

# ----------------------------------------------------------------------------
# setCronTask ()
# params:
# @script2Execute
# @time - Format (hh:mm)
# @frequency min / hourly / weekly / daily / monthly
# @user
# ----------------------------------------------------------------------------
setCronTask ()
{
   script2Execute="$1"
   time="$2"
   frequency="$3"
   user="$4"

   #wich cron do we use?   
   cron_file="$OD_CRON_PATH/$user/$OD_CRON_FILE_NAME"
   cron_line=`getCronLine "$script2Execute" "$time" "$frequency" "$user"`   
   mod="0"
   
   #1- check if file exists
   if [ ! -e "$cron_file" ]; then
      #TODO: Admin must have permissions to do this
      mkdir "$OD_CRON_PATH/$user 2> /dev/null"
      touch "$cron_file"
      chown "$user" "$cron_file"
      chmod 700 "$cron_file"
   fi

   #2- check whether the task exists
   while read line; do
      file2executeInCron=`echo "$line" | sed "s/[0-9*]* [0-9*]* [0-9*]* [0-9*]* [0-9*]* //"`	 

      if [ "$script2Execute" = "$file2executeInCron" ]; then
         # modify line
         echo "$cron_line" >> $TMP_FILE	         
         mod=1
      else
         echo "$line" >> $TMP_FILE	 	       	       
      fi

   done < "$cron_file"

   #3- if exists modify it if not add
   if [ "$mod" = 0 ]; then
      echo "$cron_line" >> $cron_file
   	rm "$TMP_FILE"
   else
      # swapeamos los ficheros
	   mv "$TMP_FILE" "$cron_file"
   fi   
	
	# We reload the cron
	reloadCron "$user"
   
}

# ----------------------------------------------------------------------------
# getCronLine ()
# params:
# @script2Execute
# @time - Format (hh:mm)
# @frequency min / hourly / weekly / daily / monthly
# @user
# ----------------------------------------------------------------------------
getCronLine ()
{
   script2Execute="$1"
   time="$2"
   frequency="$3"
   cron_line=""
   
   #hours and minutes
   hours=`echo $time | cut -d":" -f1`
   minutes=`echo $time | cut -d":" -f2`   
   
   case "$frequency" in
      min)
		   cron_line="* * * * * $script2Execute";;
	   hourly)
		   cron_line="$minutes * * * * $script2Execute";;
	   daily)
		   cron_line="$minutes $hours * * * $script2Execute";;
	   weekly)
		   cron_line="$minutes $hours * * 7 $script2Execute";;
	   monthly)
		   cron_line="$minutes $hours 1 * * $script2Execute";;
   esac

   echo "$cron_line";
}

reloadCron ()
{
	user=$1
	# Reset the temporary file
	TMP_FILE="/tmp/od_$RANDOM"
   cron_file="$OD_CRON_PATH/$user/$OD_CRON_FILE_NAME"

	# backup of the current jobs
	for cron in `ls $OD_CRON_PATH/$user | grep -v $OD_CRON_FILE_NAME`; do
		cat $OD_CRON_PATH/$user/$cron >> $TMP_FILE
	done
	
	# joining the two files
	cat $cron_file >> $TMP_FILE

	#4- Set the crontab to the scpecified user
	/usr/bin/crontab "$TMP_FILE" -u "$user"
	rm $TMP_FILE
}

# ----------------------------------------------------------------------------
# removeCronTask ()
# params:
# @script2Execute
# @user
# ----------------------------------------------------------------------------
removeCronTask ()
{
   script2Remove="$1"
   user="$2"

   #wich cron do we use?   
   cron_file="$OD_CRON_PATH/$user/$OD_CRON_FILE_NAME"
	mod=0
   #2- check whether the task exists
   while read line; do
      file2executeInCron=`echo "$line" | sed "s/[0-9*]* [0-9*]* [0-9*]* [0-9*]* [0-9*]* //"`	 
      if [ "$file2executeInCron" != "$script2Remove" ]; then
         echo "$line" >> $TMP_FILE	 	       	       
			mod=1
      fi
   done < "$cron_file"

   #3- if exists modify it if not add
   if [ "$mod" = 1 ]; then
      # swapeamos los ficheros
	   mv "$TMP_FILE" "$cron_file"
   fi   

	reloadCron "$user"
}





